package com.fysports.web.controller.system;

import com.fysports.web.config.asyn.AsynInitParamCache;
import com.fysports.web.controller.AbstractBaseController;
import com.fysports.web.controller.engine.RestFulController;
import com.fysports.web.kit.commons.PathBizKit;
import com.fysports.web.model.system.SystemDict;
import com.fysports.web.module.core.DefaultModuleBuilder;
import com.fysports.web.module.core.IModuleFactory;
import com.fysports.web.module.view.JsonView;
import com.fysports.web.module.view.OperateType;


/**
 * 系统字典管理
 * Created by konbluesky
 * Date : 14-8-20 下午3:17
 * Project : JdmData_WebServer
 */
public class ManagerDictController extends AbstractBaseController implements RestFulController {
    IModuleFactory moduleFactory = new DefaultModuleBuilder();


    public void index() {
        setAttr("list", SystemDict.dao.search(SystemDict.COL_LIVEFLAG, "1"));
        moduleFactory.create(this);
        execute();
        render(PathBizKit.getPageAdminPath("system/dict/dict_list.ftl"));
    }

    @Override
    public void add() {
        setAttr("m", new SystemDict());
        render(PathBizKit.getPageAdminPath("system/dict/dict_add.ftl"));
    }

    @Override
    public void edit() {
        String uuid = getPara();
        SystemDict eo = SystemDict.dao.findById(uuid);
        setAttr("m", eo);
        render(PathBizKit.getPageAdminPath("system/dict/dict_update.ftl"));
    }

    @Override
    public void save() {
        SystemDict eo = getModel(SystemDict.class, "m");
        boolean status = eo.save();
        JsonView json = new JsonView().setStatus(status);
        json.setMsg(status ? OperateType.SAVE_SUCCESS : OperateType.SAVE_FAIL);

        /*  数据库缓存刷新   */
        AsynInitParamCache.pingtai_cacheDict();

        renderJson(json);
    }

    @Override
    public void update() {
        SystemDict sm = getModel(SystemDict.class, "m");
        boolean status = sm.update();
        JsonView json = new JsonView();
        json.setStatus(status);
        json.setMsg(status ? OperateType.UPDATE_SUCCESS : OperateType.UPDATE_FAIL);

        /*  数据库缓存刷新   */
        AsynInitParamCache.pingtai_cacheDict();

        renderJson(json);
    }

    @Override
    public void delete() {
        String uuid = getPara();
        JsonView json = new JsonView();
        SystemDict sm = SystemDict.dao.findById(uuid);
        boolean status = false;
        if (sm != null) {
            sm.set(SystemDict.COL_LIVEFLAG, "0");
            status = sm.update();
        }
        json.setStatus(status);
        json.setMsg(status ? OperateType.OPERATE_SUCCESS : OperateType.OPERATE_FAIL);
        renderJson(json);
    }
}
